import axios from 'axios';

const instance = axios.create({
  baseURL: 'http://localhost:8000',
  timeout: 5000,
  headers: {
    'Content-type': 'application/json;charset=UTF-8'
  }
});
instance.interceptors.request.use(
  config => {  
    // 在发送请求之前做些什么，比如添加token到headers  
    if (localStorage.getItem('token')) {  
      config.headers['blog-auth'] = localStorage.getItem('blog-token');  
    }  
    return config;  
  },  
  error => {  
    // 对请求错误做些什么  
    console.log(error); // for debug  
    return Promise.reject(error);  
  }  
)
async function request(method, url, data, isUrl) {
  try {
    const response = null;
    if (isUrl) {
      response = await instance({
        method: method,
        url: url,
        params: data
      });
    } else {
      response = await instance({
        method: method,
        url: url,
        data: data
      });
    }
    return response.data;
  } catch (error) {
    // 处理请求错误  
    console.error(error);
    throw error;
  }
}
export default request;